#include "include.h"

/****************************************************************
 *This function is thread22
 *	(1)create socket,listen port,process command
 ****************************************************************/
void *thread22(void *str)
{	
	unsigned char write_to_syslog_buf[512];
	unsigned char valid_data_buf[256];
	unsigned int temp_pcm_all_space, temp_pcm_use_space;
	unsigned int temp_log_all_space, temp_log_use_space;
	unsigned int pcm_disk_judgement = 0;

	fprintf(stderr,"thread twenty_two running\n");

	while(1){
		if(start_system_monitor_flag == START_SYSTEM_MONITOR){
			// 获取PCM数据存储空间
			temp_pcm_all_space = get_disk_all_space((unsigned char *)PCM_SPACE_PATH);
			temp_pcm_use_space = get_disk_use_space((unsigned char *)PCM_SPACE_PATH);

			// 获取日志数据存储空间
			temp_log_all_space = get_disk_all_space((unsigned char *)PCM_LOG_PATH);
			temp_log_use_space = get_disk_use_space((unsigned char *)PCM_LOG_PATH);

			//fprintf(stderr,"pcm store space, left space : %d (MByte), used space : %d (MByte)!\n", temp_pcm_left_space, temp_pcm_use_space);
			//fprintf(stderr,"log store space, left space : %d (MByte), used space : %d (MByte)!\n", temp_log_left_space, temp_log_use_space);

			// 获取操作锁
			pthread_mutex_lock(&spaceMutex);

			// 填充PCM总空间和已使用空间
			pcm_all_space = temp_pcm_all_space - PCM_LEFT_LIMIT;
			pcm_use_space = temp_pcm_use_space;

			// 填充日志总空间和已使用空间
			log_all_space = temp_log_all_space - LOG_LEFT_LIMIT;
			log_use_space = temp_log_use_space;

			// 释放互斥锁
			pthread_mutex_unlock(&spaceMutex);

			// 使能存储时，才进行数据存满停止判读
			if(pcm_store_status == START_PCM_STORE){
				// 判断PCM剩余空间是否足够，剩余空间不够，则停止存储
				if((temp_pcm_all_space - temp_pcm_use_space - 3) <= (PCM_LEFT_LIMIT)){
					// 清空超时计数
					pcm_disk_judgement = 0;
					while(1){
						// 获取PCM数据存储空间
						temp_pcm_all_space = get_disk_all_space((unsigned char *)PCM_SPACE_PATH);
						temp_pcm_use_space = get_disk_use_space((unsigned char *)PCM_SPACE_PATH);

						// 之前获取数据有问题，不停止
						if((temp_pcm_all_space - temp_pcm_use_space - 3) > (PCM_LEFT_LIMIT)){
							break;
						}

						// 超时退出机制
						pcm_disk_judgement ++;
						if(pcm_disk_judgement > 10){
							break;
						}
						usleep(100000);
					}

					// 磁盘空间已满，停止存储
					if(pcm_disk_judgement > 10){
						// 配置标志为停止存储
						pcm_store_status = STOP_PCM_STORE;

						// 延时1s，关闭存储文件
						sleep(1);
						close(pcm_store_fd);
						pcm_store_fd = 0;
					}
				}
			}

			// 清空局部变量
			memset(write_to_syslog_buf, 0x0, sizeof(write_to_syslog_buf));

			// 获取系统时间
			create_time_buffer(write_to_syslog_buf);

			// 填充记录信息
			memset(valid_data_buf, 0x0, sizeof(valid_data_buf));
			sprintf((ViChar *)valid_data_buf, "store flag: %x, all pcm space: %d(Mbyte), used pcm space: %d(Mbyte), all log space: %d(Mbyte), used log space: %d(Mbyte)\n", \
				pcm_store_status, pcm_all_space, temp_pcm_use_space, log_all_space, log_use_space);

			// 将有效信息写到日志缓冲区中
			strcat((ViChar *)write_to_syslog_buf, (ViChar *)valid_data_buf);

			// 将信息发送给日志线程
			sendto(sockfd_wr_log, write_to_syslog_buf, strlen((ViChar *)write_to_syslog_buf) ,IPC_NOWAIT, \
					(struct sockaddr*)(&server_addr_wr_log), sizeof(struct sockaddr_in));

			fprintf(stderr,"log: %x,  %d,  %d,  %lld,  %lld,  %lld,  %lld,  %lld,  %lld,  %lld,  %lld\n", \
				pcm_store_status, pcm_store_fd, log_store_fd, single_store_len, \
				thread_pcm_store_len, all_store_data_len, sys_log_store_len, \
				inter_log_time, inter_log_wr_time, inter_pcm_time, inter_pcm_wr_time);
		}

		// 获取磁盘存储容量
		sleep(1);
	}
	
	return NULL;
}